/**
 * Created by xuweijie on 17-5-11.
 */
layui.use('form', function(){
    var $ = layui.jquery, form = layui.form();
});

//日期选择
layui.use('laydate', function(){
    var laydate = layui.laydate;

    var start = {
        min: laydate.now()
        ,max: '2099-06-16 23:59:59'
        ,istoday: false
        ,choose: function(datas){
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

    var end = {
        min: laydate.now()
        ,max: '2099-06-16 23:59:59'
        ,istoday: false
        ,choose: function(datas){
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };

    // document.getElementById('LAY_demorange_s').onclick = function(){
    //     start.elem = this;
    //     laydate(start);
    // }
    // document.getElementById('LAY_demorange_e').onclick = function(){
    //     end.elem = this
    //     laydate(end);
    // }
});

//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active={
        //查看详情
        look : function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "顾客详情",
                shadeClose: true,
                content: 'customerdetail?customerid='+othis.customerid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        },
        delete: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/deletecustomer.action",
                        type: 'post',
                        data: {
                            customerid: othis.customerid,
                            del_flag:"1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "1") {
                                layer.msg("删除成功");
                                window.location.href = "/admin/customer/showdetail";
                            }
                            layer.msg("删除失败");
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        freeze: function (othis) {
            //冻结/解冻
            var content;
            if(othis.freeze_flag=="0"){
                content="是否解冻该用户?";
            }else {
                content="是否冻结该用户?";
            }
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div id="notices" style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+content+'<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/freezecustomer.action",
                        type: 'post',
                        data: {
                            customerid: othis.customerid,
                            freeze_flag:othis.freeze_flag
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "1") {
                                layer.msg("已解冻该用户");
                                window.location.href = "/admin/customer/showdetail";
                            }else if(data=="2"){
                                layer.msg("已冻结该用户");
                                window.location.href = "/admin/customer/showdetail";
                            }else if(data=="3"){
                                layer.msg("解冻失败");
                            }else{
                                layer.msg("冻结失败");
                            }
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        deleteaddr: function (othis) {
            //删除地址
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/deladdr.action",
                        type: 'post',
                        data: {
                            addressid: othis.addressid,
                            del_flag:"1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            if (data === "0") {
                                window.location.href = '/admin/customer/customerdetail?customerid='+othis.customerid;
                                layer.msg("删除成功");
                            }else{
                                layer.msg("删除失败");
                            }
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        //查看购物车
        buycar : function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "购物车",
                shadeClose: true,
                content: '/admin/buycars/showbuycar?customerid='+othis.customerid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        }
    };
    $('.lookcus').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.customerid = $(this).attr('customerid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.delcus').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.customerid = $(this).attr('customerid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.freezecus').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.customerid = $(this).attr('customerid');
        othis.freeze_flag=$(this).attr('freeze_flag');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.deladdr').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.addressid = $(this).attr('addressid');
        othis.customerid = $(this).attr('customerid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.lookbuycar').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.customerid = $(this).attr('customerid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

//分页
layui.use(['laypage', 'layer'], function(){
    var laypage = layui.laypage,layer = layui.layer;
    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                if(jsonObject.length>0){
                    console.log(jsonObject);
                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex='+pageIndex +'&';
                    condition+="croCustomerVo.username="+objs.username+'&'+"croCustomerVo.type="+objs.type+'&'+"croCustomerVo.status="+objs.status+'&'+"croCustomerVo.email="+objs.email+'&'+
                        "croCustomerVo.startdate="+objs.startdate+'&'+"croCustomerVo.enddate="+objs.enddate+'&'+"croCustomerVo.phone="+objs.phone;
                    console.log(condition);
                    window.location.href ="/admin/customer/querylist?"+condition;
                } else {
                    window.location.href ="/admin/customer/showdetail?pageIndex="+pageIndex;
                }
            }
        }
    });
});

layui.use(['laypage', 'layer'], function() {
    var laypage = layui.laypage, layer = layui.layer;
    //地址分页
    laypage({
        cont: 'detailpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            var cusid=$('#croCustomerid').val();
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                window.location.href ="/admin/customer/customerdetail?pageIndex="+pageIndex+'&customerid='+cusid;
            }
        }
    });
});
